{# Copyright The IETF Trust 2015-2022, All Rights Reserved #}
{% load origin %}
{% origin %}
{% load ietf_filters %}
{% load textfilters %}
<div class="row">
    <div class="col-md-2">
        {% for n, positions in position_groups %}
            <p class="h5">
                <span class="badge rounded-pill {{ n|pos_to_label_format }}">{{ n.name }}</span>
            </p>
            {% for p in positions %}
                <div class="balloter-name mb-2">
                    {% if p.is_old_pos %}<span class="text-body-secondary">({% endif %}{% if p.comment or p.discuss %}<a href="#{{ doc.name|slugify }}_{{ p.balloter.plain_name|slugify }}">{% endif %}{{ p.balloter.plain_name }}{% if p.comment or p.discuss %}</a>{% endif %}{% if p.is_old_pos %})</span>{% endif %}
                </div>
            {% empty %}
                <span class="text-body-secondary">(None)</span>
            {% endfor %}
            <br>
        {% endfor %}
    </div>
    <div class="col-md-10">
        {% if all_ballots and all_ballots|length > 1 %}
	        <label class="my-1 fw-bold">Ballots:</label>
            <ul class="pagination pagination-sm flex-wrap">
                {% for b in all_ballots %}
                    <li class="page-item{% if b == ballot %} active{% endif %}">
                        <a class="page-link" href="{% url "ietf.doc.views_doc.document_ballot" name=doc.name ballot_id=b.pk %}">
                            {{ b.ballot_type.name }} ({{ b.rev }})
                        </a>
                    </li>
                {% endfor %}
            </ul>
        {% endif %}
        {% if doc.type_id == "draft" or doc.type_id == "conflrev" or doc.type_id == "statchg" %}
            {% if deferred %}
                <p class="alert alert-danger my-3">
                    Ballot deferred by {{ deferred.by }} on {{ deferred.time|date:"Y-m-d" }}.
                </p>
            {% endif %}
        {% endif %}
        {% if not ballot_open or iesg_state == "ann" or iesg_state == "approved" %}
            <p class="alert alert-warning my-3">
                <b>Note:</b> This ballot was opened for revision {{ ballot.rev }} and is now closed.
            </p>
        {% else %}
            <p class="alert alert-info my-3">
                <b>Summary:</b> {{ summary }}
            </p>
        {% endif %}
        {% if ballot.ballot_type.question %}
            <p class="alert alert-secondary my-3">
                <b>Ballot question:</b> "{{ ballot.ballot_type.question }}"
            </p>
        {% endif %}
        {% if editable and user|has_role:"Area Director,Secretariat,IRSG Member,RSAB Member" %}
            <a class="btn btn-primary my-3"
               href="https://mailarchive.ietf.org/arch/search/?q=subject:{{ doc.name }}+AND+subject:(discuss+OR+comment+OR+review+OR+concern)">
                Search Mailarchive
            </a>
            {% if user|can_ballot:doc %}
                <a class="btn btn-primary"
                   href="{% url "ietf.doc.views_ballot.edit_position" name=doc.name ballot_id=ballot.pk %}?ballot_edit_return_point={{ request.path|urlencode }}">
                    Edit position
                </a>
            {% endif %}
            {% if user|can_defer:doc %}
                {% if deferred %}
                    <a class="btn btn-warning"
                    href="{% url 'ietf.doc.views_ballot.undefer_ballot' name=doc.name %}">Undefer ballot</a>
                {% else %}
                    {% if doc.telechat_date %}
                        <a class="btn btn-warning"
                        href="{% url 'ietf.doc.views_ballot.defer_ballot' name=doc.name %}">Defer ballot</a>
                    {% endif %}
                {% endif %}
            {% endif %}
            {% if user|can_clear_ballot:doc %}
                    <a class="btn btn-danger"
                    href="{% url 'ietf.doc.views_ballot.clear_ballot' name=doc.name ballot_type_slug=ballot.ballot_type.slug %}">
                        Clear ballot
                    </a>
            {% endif %}
        {% endif %}
        {% for n, positions in position_groups %}
            {% for p in positions %}
                {% if not p.is_old_pos %}
                    <div class="h5 d-flex mt-4"
                         id="{{ doc.name|slugify }}_{{ p.balloter.plain_name|slugify }}">
                        <div class="flex-fill">
                            {{ p.balloter.plain_name }}
                        </div>
                        <div class="flex-fill text-end">
                            {% if p.old_positions %}<span class="text-body-secondary small">(was {{ p.old_positions|join:", " }})</span>{% endif %}
                            <span class="badge rounded-pill {{ p.pos|pos_to_label_format }}">{{ p.pos }}</span>
                            {% if user|has_role:"Secretariat" %}
                                <a href="{% url "ietf.doc.views_ballot.edit_position" name=doc.name ballot_id=ballot.pk %}?balloter={{ p.balloter.pk }}"
                                   title="Click to edit the position of {{ p.balloter.plain_name }}"
                                   class="btn btn-primary btn-sm">
                                    Edit
                                </a>
                            {% endif %}
                        </div>
                    </div>
                    {% if p.pos.blocking and p.discuss %}
                        <div class="card border-danger {% if p.comment %} mb-2{% endif %}">
                            <div class="card-header text-bg-danger">
                                <div>
                                    <b>{{ p.pos.name }}</b>
                                    ({{ p.discuss_time|date:"Y-m-d" }}{% if not p.for_current_revision and p.get_dochistory.rev %}{% if p.discuss_time %} {% endif %}for -{{ p.get_dochistory.rev }}{% endif %})
                                    {# TODO: This logic is in place for discusses/ballots/comments, consider centralizing somewhere #}
                                    {% if p.send_email %}
                                        <span class="badge rounded-pill text-bg-success float-end"
                                              title="Email requested to be sent for this discuss">
                                            <i class="bi bi-envelope-fill"></i> Sent
                                        </span>
                                    {% elif p.any_email_sent == True %}
                                        <span class="badge rounded-pill text-bg-secondary float-end"
                                              title="Email requested to be sent for earlier discuss">
                                            <i class="bi bi-envelope-fill"></i> Sent for earlier
                                        </span>
                                    {% elif p.any_email_sent == False %}
                                        <span class="badge rounded-pill text-bg-danger float-end"
                                              title="No email send requests for this discuss">
                                            <i class="bi bi-envelope-fill"></i> Not sent
                                        </span>
                                    {% else %}
                                        <span class="badge rounded-pill text-bg-light float-end"
                                              title="No discuss send log available">
                                            <i class="bi bi-envelope-fill"></i> Unknown
                                        </span>
                                    {% endif %}
                                </div>
                            </div>
                            <div class="card-body">
                                <pre class="ballot pasted">{{ p.discuss|escape|urlize_ietf_docs|linkify }}</pre>
                            </div>
                        </div>
                    {% endif %}
                    {% if p.comment %}
                        <div class="card {{ p.pos|pos_to_border_format }}">
                            <div class="card-header {{ p.pos|pos_to_label_format }}">
                                <div>
                                    <b>Comment</b>
                                    ({{ p.comment_time|date:"Y-m-d" }}{% if not p.for_current_revision and p.get_dochistory.rev %}{% if p.comment_time %} {% endif %}for -{{ p.get_dochistory.rev }}{% endif %})
                                    {% if p.send_email %}
                                        <span class="badge rounded-pill text-bg-success float-end"
                                              title="Email requested to be sent for this comment">
                                            <i class="bi bi-envelope-fill"></i> Sent
                                        </span>
                                    {% elif p.any_email_sent == True %}
                                        <span class="badge rounded-pill text-bg-secondary float-end"
                                              title="Email requested to be sent for earlier comment">
                                            <i class="bi bi-envelope-fill"></i> Sent for earlier
                                        </span>
                                    {% elif p.any_email_sent == False %}
                                        <span class="badge rounded-pill text-bg-danger float-end"
                                              title="No email send requests for this comment">
                                            <i class="bi bi-envelope-fill"></i> Not sent
                                        </span>
                                    {% else %}
                                        <span class="badge rounded-pill text-bg-light float-end"
                                              title="No comment send log available">
                                            <i class="bi bi-envelope-fill"></i> Unknown
                                        </span>
                                    {% endif %}
                                </div>
                            </div>
                            <div class="card-body">
                                <pre class="ballot pasted">{{ p.comment|escape|urlize_ietf_docs|linkify }}</pre>
                            </div>
                        </div>
                    {% endif %}
                {% endif %}
            {% endfor %}
        {% endfor %}
        {% for n, positions in position_groups %}
            {% for p in positions %}
                {% if p.is_old_pos %}
                    <div class="h5 d-flex mt-4"
                         id="{{ doc.name|slugify }}_{{ p.balloter.plain_name|slugify }}">
                        <div class="flex-fill text-body-secondary">
                            {{ p.balloter.plain_name }} <span class="badge rounded-pill text-bg-secondary">Former IESG member</span>
                        </div>
                        <div class="flex-fill text-end">
                            {% if p.old_positions %}<span class="text-body-secondary small">(was {{ p.old_positions|join:", " }})</span>{% endif %}
                            <span class="badge rounded-pill {{ p.pos|pos_to_label_format }}">{{ p.pos }}</span>
                            {% if user|has_role:"Secretariat" %}
                                <a href="{% url "ietf.doc.views_ballot.edit_position" name=doc.name ballot_id=ballot.pk %}?balloter={{ p.balloter.pk }}"
                                   title="Click to edit the position of {{ p.balloter.plain_name }}"
                                   class="btn btn-primary btn-sm">
                                    Edit
                                </a>
                            {% endif %}
                        </div>
                    </div>
                    <div class="card {{ p.pos|pos_to_border_format }}">
                        <div class="card-header {{ p.pos|pos_to_label_format }}">
                            <div>
                                <b>{{ p.pos.name }} </b>
                                {% if p.pos.blocking and p.discuss %}
                                    <b>[Treat as non-blocking comment]</b>
                                    ({{ p.discuss_time|date:"Y-m-d" }}{% if not p.for_current_revision and p.get_dochistory.rev %}{% if p.discuss_time %} {% endif %}for -{{ p.get_dochistory.rev }}{% endif %})
                                {% else %}
                                    ({{ p.comment_time|date:"Y-m-d" }}{% if not p.for_current_revision and p.get_dochistory.rev %}{% if p.comment_time %} {% endif %}for -{{ p.get_dochistory.rev }}{% endif %})
                                {% endif %}
                                {% if p.send_email %}
                                    <span class="badge rounded-pill text-bg-success float-end"
                                          title="Email requested to be sent for this ballot position">
                                        <i class="bi bi-envelope-fill"></i> Sent
                                    </span>
                                {% elif p.any_email_sent == True %}
                                    <span class="badge rounded-pill text-bg-secondary float-end"
                                            title="Email requested to be sent for earlier ballot position">
                                        <i class="bi bi-envelope-fill"></i> Sent for earlier
                                    </span>
                                {% elif p.any_email_sent == False %}
                                    <span class="badge rounded-pill text-bg-danger float-end"
                                          title="No email send requests for this ballot position">
                                        <i class="bi bi-envelope-fill"></i> Not sent
                                    </span>
                                {% else %}
                                    <!-- <i class="bi bi-envelope-slash float-end"
                                          title="No ballot position send log available"></i> -->
                                    <span class="badge rounded-pill text-bg-light float-end"
                                          title="No ballot position send log available">
                                        <i class="bi bi-envelope-fill"></i> Unknown
                                    </span>
                                {% endif %}
                            </div>
                        </div>
                        {% if p.pos.blocking and p.discuss %}
                            <div class="card-body">
                                <pre class="ballot pasted">{{ p.discuss|escape|urlize_ietf_docs|linkify }}</pre>
                            </div>
                        {% else %}
                            <div class="card-body">
                                <pre class="ballot pasted">{{ p.comment|escape|urlize_ietf_docs|linkify }}</pre>
                            </div>
                        {% endif %}
                    </div>
                {% endif %}
            {% endfor %}
        {% endfor %}
    </div>
</div>